<?php
/**
 * admin service file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/04
 * Time: 17:52
 */
 
namespace Modules\Admin\Service;

use HiCommon\Repository\OrderCheckpointRepository;
use HiCommon\Repository\ShopRepository;

class OrderCheckpointService
{

    /**
     * 获取所有权限
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all(){
        try {
            $arr_option = ['order'=>['key'=>'id','value'=>'asc']];
            $data = OrderCheckpointRepository::get_all($arr_option)->toarray();
            return  hello_success('成功',$data);
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 获取分页数据
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list( array $arr_search )
    {
        try {

            //获取当前登录用户的信息
            $admin_user = admin_user();
            if( $admin_user['type'] != 1 && $admin_user['type'] != 2 ){
                //获取当前商户的商户id
                $shop = ShopRepository::get_one_by_where( (array)[ 'admin_id' => $admin_user['id'] ]  );
                $arr_option['where'] = ['shop_id'=> $shop['id']];
            }
            $arr_option['order'] = ['key'=>'id','value'=>'desc'];
            $arr_option['with'] = ['shop','user'];

            $data = OrderCheckpointRepository::get_list($arr_option , (array) $arr_search );

            $main_types = [ '1' => '个人' , '2' => '公司' ];
            $payment_types = [ 'alipay' => '支付宝' , 'wechat_pay' => '微信' ];
            $pay_types = [ '1' => '未支付' , '2' => '已支付' ];
            $appointment_types = [ '1' => '预约中' , '2' => '预约成功' , '3' => '预约失败'];
            $data->appends($arr_search);
            $data = list_page_data($data , $arr_search );
             foreach ($data['data'] as $key => &$value) {
                if( in_array( $value['main_type'] , array_keys( $main_types ) ) ){
                    $value['main_type'] = $main_types[$value['main_type']];
                }else{
                    $value['main_type'] = '未知';
                }

                if( in_array( $value['pay_types'] , array_keys( $payment_types ) ) ){
                    $value['payment_method'] = $payment_types[$value['pay_types']];
                }else{
                    $value['payment_method'] = '未知';
                }

                if( in_array( $value['pay_state'] , array_keys( $pay_types ) ) ){
                    $value['pay_state'] = $pay_types[$value['pay_state']];
                }else{
                    $value['pay_state'] = '未知';
                }

                if( in_array( $value['appointment_state'] , array_keys( $appointment_types ) ) ){
                    $value['appointment_state'] = $appointment_types[$value['appointment_state']];
                }else{
                    $value['appointment_state'] = '未知';
                }
            }
            return  hello_success('成功',$data);
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 添加一条数据
     *
     * @param $arr_input
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        $arr_input = hi_array_del_null($arr_input);
        try {
            //添加
            $res = OrderCheckpointRepository::add_one($arr_input)->toarray();
            if(empty($res)){
                return hello_error('添加失败');
            }

            return  hello_success('成功',$res);
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id)
    {
        try {
            $n_id = (int) $n_id;
            if($n_id <= 0) {
                hello_error('数据ID不能为空');
            }

            //查询
            $res = OrderCheckpointRepository::get_one($n_id)->toarray();
            if(empty($res)){
                return hello_error('查询失败');
            }

            return  hello_success('查询成功',$res);
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        $arr_input = hi_array_del_null($arr_input);
        try {
            $arr_input['id'] = (int)$arr_input['id'];
            if ($arr_input['id'] <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = OrderCheckpointRepository::edit_one($arr_input);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 修改单个字段
     *
     * @param $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state($arr_input){
        try {
            $arr_input['id'] = (int)$arr_input['id'];
            if ($arr_input['id'] <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = OrderCheckpointRepository::edit_one($arr_input);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one($n_id)
    {
        try {
            if ($n_id <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = OrderCheckpointRepository::delete_one($n_id);
            if($res === false){
                return hello_error('操作失败');
            }
            return  hello_success('成功');

        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 获取分页数据- 商家 - 年检预约订单列表 - 全部
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function all_order_list($arr_search = [] )
    {
        $arr_search = array_trim($arr_search);
        if(strlen($arr_search['key_word']) > 0){
            $arr_search['order_sn'] = str_trim($arr_search['key_word']);
        }

        $arr_where  = array_remain($arr_search,['pay_state']);
        $arr_option['where'] = $arr_where;
        $arr_option['order'] = ['key'=>'id','value'=>'desc'];
        $arr_option['with'] = ['shop','user'];
        $data = OrderCheckpointRepository::order_all_list($arr_option , $arr_search );
        $data->appends($arr_search);
        $data = list_page_data($data , $arr_search );
        return  hello_success('成功',$data);
    }



    /**
     * 获取分页数据- 商家 - 年检预约订单列表 - 全部
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function all_order_list_by_shopid($n_shop_id = 0, $arr_search = [] )
    {
        $n_shop_id = intval($n_shop_id);
        if($n_shop_id <= 0){
            return  hello_error('商家id不能为空');
        }
        $arr_search['key_word'] = str_trim($arr_search['key_word']);
        $arr_search['search_type'] = str_trim($arr_search['search_type']);
        $arr_search['search_order_type'] = str_trim($arr_search['search_order_type']);

        $arr_option['order'] = ['key'=>'id','value'=>'desc'];
//        $arr_option['with'] = ['shop','user'];
        $data = OrderCheckpointRepository::shop_order_all_list($n_shop_id, $arr_option , $arr_search );
        $data->appends($arr_search);
        $data = list_page_data($data , $arr_search );
        return  hello_success('成功',$data);
    }

    /**
     * 获取分页数据- 商家 - 年检预约订单列表 - 待处理
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function waiting_order_list_by_shopid($n_shop_id = 0, $arr_search = [] )
    {
        $n_shop_id = intval($n_shop_id);
        if($n_shop_id <= 0){
            return  hello_error('商家id不能为空');
        }
        $arr_search['key_word'] = str_trim($arr_search['key_word']);
        if(strlen($arr_search['key_word']) > 0){
            $arr_search['order_sn'] = str_trim($arr_search['key_word']);
        }

        $arr_option['order'] = ['key'=>'id','value'=>'desc'];
        //        $arr_option['with'] = ['shop','user'];
        $data = OrderCheckpointRepository::shop_order_waiting_list($n_shop_id, $arr_option , $arr_search );
        $data->appends($arr_search);
        $data = list_page_data($data , $arr_search );
        return  hello_success('成功',$data);
    }

    /**
     * 获取分页数据- 商家 - 年检预约订单列表 - 已完成
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function complete_order_list_by_shopid($n_shop_id = 0, $arr_search = [] )
    {
        $n_shop_id = intval($n_shop_id);
        if($n_shop_id <= 0){
            return  hello_error('商家id不能为空');
        }
        $arr_search['key_word'] = str_trim($arr_search['key_word']);
        if(strlen($arr_search['key_word']) > 0){
            $arr_search['order_sn'] = str_trim($arr_search['key_word']);
        }

        $arr_option['order'] = ['key'=>'id','value'=>'desc'];
        //        $arr_option['with'] = ['shop','user'];
        $data = OrderCheckpointRepository::shop_order_complete_list($n_shop_id, $arr_option , $arr_search );
        $data->appends($arr_search);
        $data = list_page_data($data , $arr_search );
        return  hello_success('成功',$data);
    }


}